//
//  NoticeMgrController.m
//  Notice
//
//  Created by CPX on 2018/9/12.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "NoticeMgrController.h"
#import "NoticeController.h"
#import "NewsDetailController.h"
#import "NewsController.h"

/**
 通知容器页
 @author yzl
 */
@interface NoticeMgrController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *noticeBtn;
@property (nonatomic, strong) UIButton *newsBtn;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NoticeController *noticeCtr;
@property (nonatomic, strong) NoticeController *messageCtr;

@end

@implementation NoticeMgrController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initViews{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth*2, kScreenHeight);
    self.scrollView.backgroundColor = UIColorHex(#F6F6F6);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    self.messageCtr = [NoticeController new];
//    self.messageCtr.isRecommend = YES;
    [self.scrollView addSubview:self.messageCtr.view];
    [self addChildViewController:self.messageCtr];
    
    if (self.isNotice) {
        
    }
    else{
        if ([UIView appearance].semanticContentAttribute ==  UISemanticContentAttributeForceRightToLeft) {
            //        self.noticeCtr.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            self.messageCtr.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
            [self.scrollView scrollToRight];
        }
        else{
            self.messageCtr.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            //        self.noticeCtr.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
        }
    }
}

- (void)initNavigation{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, NavHeight)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    self.noticeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.noticeBtn addTarget:self action:@selector(onRecommendTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.noticeBtn setTitle:TLString(@"greeting_cards_notice_Message") forState:UIControlStateNormal];
    [self.noticeBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
    self.noticeBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [view addSubview:self.noticeBtn];
    [self.noticeBtn sizeToFit];
    
    self.newsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.newsBtn addTarget:self action:@selector(onPopularTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.newsBtn setTitle:TLString(@"greeting_cards_Notice") forState:UIControlStateNormal];
    [self.newsBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    self.newsBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [view addSubview:self.newsBtn];
    [self.newsBtn sizeToFit];
    
    self.line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 3)];
    self.line.backgroundColor = UIColorHex(#00732F);
    [view addSubview:self.line];
    
    self.noticeBtn.bottom = NavHeight - 8.5;
    self.newsBtn.bottom = NavHeight - 8.5;
    self.line.bottom = NavHeight - 8.5;
    
    CGFloat offset = 65;
    CGFloat x = view.width/2;
    if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
        self.newsBtn.centerX = x-offset;
        self.noticeBtn.centerX = x+offset;
    }
    else{
        self.noticeBtn.centerX = x-offset;
        self.newsBtn.centerX = x+offset;
    }
    self.line.centerX = self.noticeBtn.centerX;
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage RTL_ImageWithName:@"back_ic"] forState:UIControlStateNormal];
    [view addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.centerY.mas_equalTo(self.noticeBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(24, 21));
    }];
    [back addTarget:self action:@selector(onLeftButtonTouched) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - actions

- (void)onRecommendTouched{
    
    [self animatedLine:YES];
    [self.noticeBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
    [self.newsBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    [self.scrollView scrollRectToVisible:self.messageCtr.view.frame animated:YES];
}

- (void)onPopularTouched{
    [self animatedLine:NO];
    [self.noticeBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    [self.newsBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
    [self.scrollView scrollRectToVisible:self.noticeCtr.view.frame animated:YES];
}

- (void)animatedLine:(BOOL)isRecommend{
    CGRect frame = self.line.frame;
    self.noticeCtr.view.hidden = NO;
    if (isRecommend) {
        self.line.centerX = self.noticeBtn.centerX;
    }else{
        self.line.centerX = self.newsBtn.centerX;
    }
    
    CGRect frame2 = self.line.frame;
    self.line.frame = frame;
    
    [UIView animateWithDuration:.2 animations:^{
        self.line.frame = frame2;
    } completion:^(BOOL finished) {
        if (finished) {
            if (isRecommend) {
                [self.noticeBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
                [self.newsBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
            }
            else{
                [self.noticeBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
                [self.newsBtn setTitleColor:UIColorHex(#00732F) forState:UIControlStateNormal];
            }
        }
    }];
}

#pragma mark - scroll view

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0 ) {
        [self animatedLine:self.noticeBtn.centerX<self.newsBtn.centerX];
    }
    else if(scrollView.contentOffset.x == kScreenWidth){
        [self animatedLine:self.noticeBtn.centerX>self.newsBtn.centerX];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
}

#pragma mark - lazy load

- (NoticeController*)noticeCtr{
    if (!_noticeCtr) {
        _noticeCtr = [NoticeController new];
        _noticeCtr.isSystemNotice = YES;
        [self.scrollView addSubview:_noticeCtr.view];
        [self addChildViewController:_noticeCtr];
        if ([UIView appearance].semanticContentAttribute ==  UISemanticContentAttributeForceRightToLeft) {
            _noticeCtr.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        }
        else{
            _noticeCtr.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
        }
    }
    return _noticeCtr;
}

@end
